package com.gears_software.bbnetwork.views;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.CurrentBankInfoModel;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.net.avatarupload.UploadFileUtil;
import com.gears_software.common.utils.SwipeRefreshLoadListView.swipe.SwipeLayout;
//当前银行信息item
@EViewGroup(R.layout.card_item)
public class CardItemView extends LinearLayout {

	
	@ViewById
	ImageView image_icon;//图标
	
	@ViewById
	TextView txt_name;//银行名称
	
	@ViewById
	TextView txt_count;//银行卡号
	
	@ViewById
	SwipeLayout swipe;//侧滑布局
	
	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	@Bean
	ToastUI toast;
	
	Context context;
	
	Activity activity;
	public CardItemView(Context context) {
		super(context);
		this.context  = context;
	}
	
	public void bind(CurrentBankInfoModel model,Activity activity){
		this.activity = activity;
		if(model!=null){
			if(model.getBankIconUrl()!=null){
				getImage(model.getBankIconUrl());
			}
			txt_name.setText(model.getBankName());
			txt_count.setText(model.getCardNo());
		}
	}
	
	@Background
	void getImage(String url){
		Bitmap bitmap = UploadFileUtil.httpsGetBitmap(url);
		setImage(bitmap);
	}
	
	@UiThread
	void setImage(Bitmap bitmap){
		image_icon.setImageBitmap(bitmap);
	}
	//删除
	@Click
	void trash(){
		deleteCard();
	}
	
	@Background
	void deleteCard(){
		ResultModel<String> res = finder.getDeleteCard(sp.userId().get());
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			showTxt("删除成功！");
			sp.hasBankCard().put("0");
			closeView();
			activity.finish();
			
		}else{
			showTxt("删除银行卡失败！"+res.getMessage());
			closeView();
		}
	}
	
	
	@UiThread
	void closeView(){
		swipe.close();
	}
	
	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}

}
